﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace c2.闹钟事件
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Form form = new Form();
            form.clock.Tick(10);
            Console.ReadKey();
        }
    }
}


public delegate void TickHandler(object sender, TickEventArgs e);  //定义委托

public class TickEventArgs
{
    public int Tick_Time { get; set; }
}

public class Clock
{
    public event TickHandler Set_Tick;   //定义事件
        public void Tick(int time)
    {
        Console.WriteLine("Countdown：");
        TickEventArgs args = new TickEventArgs() { Tick_Time = time};
        Set_Tick(this, args);
    }
}

public class Form
{
    public Clock clock = new Clock();  //实例化
    public Form()
    {
        clock.Set_Tick += new TickHandler(Tick);   //注册Tick方法
        clock.Set_Tick += new TickHandler(Alarm);  //注册Alarm方法
    }

    void Tick(object sender, TickEventArgs args)   //嘀嗒（Tick）功能
    {
        while(args.Tick_Time > 0)
        {
            Console.WriteLine ($"Ticking!!! [Rest_Time：{args.Tick_Time}s]");
            System.Threading.Thread.Sleep(1000);
            args.Tick_Time -= 1;
        }
    }

    void Alarm(object sender, TickEventArgs args)   //响铃（Alarm）功能
    {
        while(args.Tick_Time == 0)
        {
            Console.WriteLine("Alarming!!!");
            args.Tick_Time = -1;
        }
    }
}